//
//  BuyLotteryViewController.m
//  MoXiProject
//
//  Created by 沈晨宇 on 2017/6/29.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "BuyLotteryViewController.h"
#import "BuyLotteryViewModel.h"
#import "MXTabViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "NearViewController.h"
@interface BuyLotteryViewController ()
{
    
    NSString* lotteryNum;
    NSString* BuyNum;
    NSString* redBall;
    NSString* blueBall;
    
}
@property(nonatomic,strong)BuyLotteryViewModel * model;
@end

@implementation BuyLotteryViewController


-(BuyLotteryViewModel *) model{
    if (!_model) {
        _model = [[BuyLotteryViewModel alloc] init];
    }
    return _model;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.R_Array.count==0) {
        
        redBall=[self.numArray componentsJoinedByString:@","];
        NSLog(@"lotteryNum:%@",redBall);
        blueBall=@"";
        NSLog(@"blueBall:%@",blueBall);
        lotteryNum=[self.numArray componentsJoinedByString:@","];
        
    }else{
        
        redBall=[self.R_Array componentsJoinedByString:@","];
        NSLog(@"redBall:%@",redBall);
        blueBall=[self.B_Array componentsJoinedByString:@","];
        NSLog(@"blueBall:%@",blueBall);
        
        lotteryNum=[NSString stringWithFormat:@"%@,%@",redBall,blueBall];
        //        NSLog(@"lotteryNum:%@",lotteryNum);
        
    }
    
    [self initNav];
    BuyNum=@"1";
    [self getNum];
    [self initUI];
    NSLog(@"%@",self.lotteryType);
    
    
    
}

-(void)getNum{
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    
    [[self.model LotteryBuyNumParameter:self.lotteryType] subscribeNext:^(id result) {
        
        if([result[@"errorcode"] integerValue] == 0){
            
            BuyNum=result[@"result"][@"qihao"];
            NSLog(@"======%@",BuyNum);
            
            
        }else{
            
            [MXUtility showTips:@"获取期号失败"];
            
        }
        
        [MXUtility hideMBProgress:self.view];
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
        
    }];
    
}

-(void)initNav{
    
    [self.navigationItem setTitle:@"收藏"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    
}

-(void)initUI{
    
    UIView* bgView=[UIView new];
    bgView.backgroundColor=[UIColor whiteColor];
    bgView.layer.borderWidth = 1;
    bgView.layer.borderColor = [DEF_COLOR_F0F0F0 CGColor];
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-20);
        make.height.mas_equalTo(153);
        make.top.equalTo(self.view).with.offset(20);
        make.leading.equalTo(self.view).with.offset(10);
    }];
    
    UILabel* tittle=[UILabel new];
    tittle.text=self.titleStr;
    tittle.textAlignment=NSTextAlignmentCenter;
    tittle.font=DEF_MyFont(15);
    tittle.textColor=baseBlackColor;
    [bgView addSubview:tittle];
    [tittle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-20);
        make.height.mas_equalTo(15);
        make.top.equalTo(bgView).with.offset(12);
        make.leading.equalTo(bgView).with.offset(0);
    }];
    
    UIView* line1=[UIView new];
    line1.backgroundColor=DEF_COLOR_F0F0F0;
    [bgView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-20);
        make.height.mas_equalTo(1);
        make.top.equalTo(tittle.mas_bottom).with.offset(12);
        make.leading.equalTo(bgView).with.offset(0);
    }];
    
    UILabel* xuanhao=[UILabel new];
    xuanhao.text=@"选号";
    xuanhao.textAlignment=NSTextAlignmentLeft;
    xuanhao.font=DEF_MyFont(13);
    xuanhao.textColor=baseBlackColor;
    [bgView addSubview:xuanhao];
    [xuanhao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(13);
        make.top.equalTo(line1.mas_bottom).with.offset(12);
        make.leading.equalTo(bgView).with.offset(15);
    }];
    
    UIView* line0=[UIView new];
    line0.backgroundColor=DEF_COLOR_F0F0F0;
    [bgView addSubview:line0];
    [line0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(113);
        make.top.equalTo(line1).with.offset(0);
        make.leading.equalTo(xuanhao.mas_trailing).with.offset(10);
    }];
    
    UILabel* xuanhaoLable=[UILabel new];
    xuanhaoLable.text=lotteryNum;
    xuanhaoLable.textAlignment=NSTextAlignmentLeft;
    xuanhaoLable.font=DEF_MyFont(13);
    xuanhaoLable.textColor=baseLightBlackColor;
    [bgView addSubview:xuanhaoLable];
    [xuanhaoLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(13);
        make.top.equalTo(line1.mas_bottom).with.offset(12);
        make.leading.equalTo(line0.mas_trailing).with.offset(5);
    }];
    
    UIView* line11=[UIView new];
    line11.backgroundColor=DEF_COLOR_F0F0F0;
    [bgView addSubview:line11];
    [line11 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-20);
        make.height.mas_equalTo(1);
        make.top.equalTo(xuanhao.mas_bottom).with.offset(12);
        make.leading.equalTo(bgView).with.offset(0);
    }];
    
    UILabel* wanfa=[UILabel new];
    wanfa.text=@"玩法";
    wanfa.textAlignment=NSTextAlignmentLeft;
    wanfa.font=DEF_MyFont(13);
    wanfa.textColor=baseBlackColor;
    [bgView addSubview:wanfa];
    [wanfa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(13);
        make.top.equalTo(line11.mas_bottom).with.offset(12);
        make.leading.equalTo(bgView).with.offset(15);
    }];
    
    UILabel* wanfaLable=[UILabel new];
    wanfaLable.text=self.wanfaStr;
    wanfaLable.textAlignment=NSTextAlignmentLeft;
    wanfaLable.font=DEF_MyFont(13);
    wanfaLable.textColor=baseLightBlackColor;
    [bgView addSubview:wanfaLable];
    [wanfaLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(13);
        make.top.equalTo(line11.mas_bottom).with.offset(12);
        make.leading.equalTo(line0.mas_trailing).with.offset(5);
    }];
    
    UIView* line111=[UIView new];
    line111.backgroundColor=DEF_COLOR_F0F0F0;
    [bgView addSubview:line111];
    [line111 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-20);
        make.height.mas_equalTo(1);
        make.top.equalTo(wanfa.mas_bottom).with.offset(12);
        make.leading.equalTo(bgView).with.offset(0);
    }];
    
    UILabel* jiage=[UILabel new];
    jiage.text=@"积分";
    jiage.textAlignment=NSTextAlignmentLeft;
    jiage.font=DEF_MyFont(13);
    jiage.textColor=baseBlackColor;
    [bgView addSubview:jiage];
    [jiage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(13);
        make.top.equalTo(line111.mas_bottom).with.offset(12);
        make.leading.equalTo(bgView).with.offset(15);
    }];
    
    UILabel* jiageLable=[UILabel new];
    jiageLable.text=@"收藏成功送5积分";
    jiageLable.textAlignment=NSTextAlignmentLeft;
    jiageLable.font=DEF_MyFont(13);
    jiageLable.textColor=baseLightBlackColor;
    [bgView addSubview:jiageLable];
    [jiageLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(13);
        make.top.equalTo(line111.mas_bottom).with.offset(12);
        make.leading.equalTo(line0.mas_trailing).with.offset(5);
    }];
    
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = DEF_COLOR_F0F0F0;
    [self.view addSubview:line2];
    
    if(iPhoneX){
        
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(-52-34);
            make.size.mas_equalTo(CGSizeMake(SCREEN_SIZE.width, 1));
        }];
        
    }else{
        
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(-52);
            make.size.mas_equalTo(CGSizeMake(SCREEN_SIZE.width, 1));
        }];
        
    }
    
    
    UIImageView* clearImage=[UIImageView new];
    clearImage.image=[UIImage imageNamed:@"LJT_"];
    [self.view addSubview:clearImage];
    [clearImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(22);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-25);
        make.leading.equalTo(self.view).with.offset(12);
    }];
    
    if(iPhoneX){
        
        [clearImage mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.width.mas_equalTo(20);
            make.height.mas_equalTo(22);
            make.bottom.equalTo(self.view.mas_bottom).with.offset(-25-34);
            make.leading.equalTo(self.view).with.offset(12);
        }];
        
    }else{
        
        [clearImage mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.width.mas_equalTo(20);
            make.height.mas_equalTo(22);
            make.bottom.equalTo(self.view.mas_bottom).with.offset(-25);
            make.leading.equalTo(self.view).with.offset(12);
        }];
        
    }
    
    UILabel* faxed=[UILabel new];
    faxed.text=@"清空";
    faxed.font=[UIFont systemFontOfSize:12];
    faxed.textColor=baseLightBlackColor;
    faxed.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:faxed];
    [faxed mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(25);
        make.height.mas_equalTo(12);
        make.top.equalTo(clearImage.mas_bottom).with.offset(5+3);
        make.leading.equalTo(self.view).with.offset(10);
    }];
    
    UIButton* clearBut=[UIButton new];
    [clearBut addTarget:self action:@selector(clearButClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:clearBut];
    if(iPhoneX){
        
        [clearBut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(50);
            make.height.mas_equalTo(51);
            make.bottom.equalTo(self.view.mas_bottom).with.offset(0-34);
            make.leading.equalTo(self.view).with.offset(0);
        }];
        
    }else{
        
        [clearBut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(50);
            make.height.mas_equalTo(51);
            make.bottom.equalTo(self.view.mas_bottom).with.offset(0);
            make.leading.equalTo(self.view).with.offset(0);
        }];
        
    }
    
    UIView* line3=[UIView new];
    line3.backgroundColor=DEF_COLOR_F0F0F0;
    [self.view addSubview:line3];
    if(iPhoneX){
        
        [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(1);
            make.height.mas_equalTo(42);
            make.bottom.equalTo(self.view.mas_bottom).with.offset(-5-34);
            make.leading.equalTo(clearImage.mas_trailing).with.offset(15);
        }];
        
    }else{
        
        [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(1);
            make.height.mas_equalTo(42);
            make.bottom.equalTo(self.view.mas_bottom).with.offset(-5);
            make.leading.equalTo(clearImage.mas_trailing).with.offset(15);
        }];
        
    }
    
    
    UIButton* collectionBut=[UIButton new];
    [collectionBut setTitle:@"收藏选号" forState:UIControlStateNormal];
    collectionBut.layer.borderWidth = 1;
    collectionBut.layer.borderColor = [DEF_COLOR_MAINCOLOR CGColor];
    collectionBut.layer.cornerRadius = 5;
    collectionBut.layer.masksToBounds = YES;
    [collectionBut setTitleColor:DEF_COLOR_MAINCOLOR forState:UIControlStateNormal];
    collectionBut.titleLabel.textAlignment=NSTextAlignmentCenter;
    collectionBut.titleLabel.font=DEF_MyFont(12);
    [collectionBut addTarget:self action:@selector(collectionButClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:collectionBut];
    [collectionBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(30);
        make.top.equalTo(line2).with.offset(11);
        make.trailing.equalTo(self.view).with.offset(-15);
    }];
    
    UIButton* picBut=[UIButton new];
    [picBut setTitle:@"保存相册" forState:UIControlStateNormal];
    //    sureBut.backgroundColor=DEF_COLOR_MAINCOLOR;
    picBut.layer.borderWidth = 1;
    picBut.layer.borderColor = [DEF_COLOR_MAINCOLOR CGColor];
    picBut.layer.cornerRadius = 5;
    picBut.layer.masksToBounds = YES;
    [picBut setTitleColor:DEF_COLOR_MAINCOLOR forState:UIControlStateNormal];
    picBut.titleLabel.textAlignment=NSTextAlignmentCenter;
    picBut.titleLabel.font=DEF_MyFont(12);
    [picBut addTarget:self action:@selector(picButClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:picBut];
    [picBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(30);
        make.centerY.equalTo(collectionBut);
        make.trailing.equalTo(collectionBut.mas_leading).with.offset(-15);
    }];
    
    UIButton* nearBut=[UIButton new];
    [nearBut setTitle:@"彩票网店" forState:UIControlStateNormal];
    nearBut.layer.borderWidth = 1;
    nearBut.layer.borderColor = [DEF_COLOR_MAINCOLOR CGColor];
    nearBut.layer.cornerRadius = 5;
    nearBut.layer.masksToBounds = YES;
    [nearBut setTitleColor:DEF_COLOR_MAINCOLOR forState:UIControlStateNormal];
    nearBut.titleLabel.textAlignment=NSTextAlignmentCenter;
    nearBut.titleLabel.font=DEF_MyFont(12);
    [nearBut addTarget:self action:@selector(nearButClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nearBut];
    [nearBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(30);
        make.centerY.equalTo(collectionBut);
        make.trailing.equalTo(picBut.mas_leading).with.offset(-15);
    }];
}

-(void)picButClick{
    
    UIImage *image = [self captureCurrentView:self.view];
    [self saveImageToPhotos:image];
}

- (UIImage *)captureCurrentView:(UIView *)view {
    
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:contextRef];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (void)saveImageToPhotos:(UIImage *)image {
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
}

-  (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error == nil) {
        [MXUtility showTips:@"保存到相册成功"];
    } else {
        [MXUtility showTips:@"保存到相册失败,请稍后再试"];
    }
}

-(void)nearButClick{
    
    NearViewController* vc=[[NearViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)clearButClick{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"是否删除选号并返回" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action){
        
    }];
    [alert addAction:cancel];
    [alert addAction:sure];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}

-(void)collectionButClick{
    
    if ([BuyNum isEqualToString:@"1"]) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"自动获取期号失败，是否重新加载" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
            
            [self getNum];
            
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action){
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alert addAction:cancel];
        [alert addAction:sure];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"是否收藏" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
            
            [[self.model BuyLotteryParameter:UserInfoShare.userCenterModel.userID type:self.lotteryType commonNum:redBall specialNum:blueBall qihao:BuyNum] subscribeNext:^(id result) {
                
                if([result[@"errorcode"] integerValue] == 0){
                    
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
                        
                        MXTabViewController *tab=[[MXTabViewController alloc]init];
                        tab.selectedIndex=0;
                        [self presentViewController:tab animated:YES completion:nil];
                        
                    }];
                    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action){
                        
                    }];
                    [alert addAction:cancel];
                    [alert addAction:sure];
                    [self presentViewController:alert animated:YES completion:nil];
                    
                }else{
                    
                    [MXUtility showTips:@"收藏失败,请稍后再试"];
                    
                }
                
                [MXUtility hideMBProgress:self.view];
            }error:^(NSError *error) {
                
                [MXUtility hideMBProgress:self.view];
                
            }];
            
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action){
            
        }];
        [alert addAction:cancel];
        [alert addAction:sure];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
}

@end
